import React from 'react';
import { WeaLocaleProvider, WeaSearchGroup } from 'ecCom';
import EType from './base/EType';
import ETitle from './base/ETitle';

const getLabel = WeaLocaleProvider.getLabel;

// 自定义元素
export default class Content_CustomElement extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { hpid, eid, ebaseid, eShareLevel, eBaseName, eTitle, eCustom = {}, synergyParams = {} } = this.props;
    const { packagename, settingcompname } = eCustom;

    let Setting4Admin = null;
    let Setting4User = null;
    if (window.rewritePortalCusEleSetting && typeof window.rewritePortalCusEleSetting == 'function') {
      const props = { ...this.props };
      const options4admin = {
        isAdmin: true,
        getInstance: setting => (this.setting4admin = setting),
        id: eid,
        eid,
        ebaseid,
        params: { hpid, eid, ebaseid },
        synergyParams
      };
      Setting4Admin = window.rewritePortalCusEleSetting(props, options4admin);

      const options4user = {
        isUser: true,
        getInstance: setting => (this.setting4user = setting),
        id: eid,
        eid,
        ebaseid,
        params: { hpid, eid, ebaseid },
        synergyParams
      };
      Setting4User = window.rewritePortalCusEleSetting(props, options4user);
    }

    if (Setting4Admin == null && window[packagename] && window[packagename]['com'] && window[packagename]['com'][settingcompname]) {
      const Setting4AdminTemp = window[packagename]['com'][settingcompname];
      Setting4Admin = <Setting4AdminTemp ecId={`${this && this.props && this.props.ecId || ''}_Setting4AdminTemp@3pfp8a`} getInstance={setting => (this.setting4admin = setting)} params={{ hpid, eid, ebaseid }} synergyParams={synergyParams} />;
    }

    return (
      <div>
        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@txumy7`} title={getLabel(81711, '基本信息')} needTigger={true} showGroup={true}>
          <EType ecId={`${this && this.props && this.props.ecId || ''}_EType@75mt9v`} ref="eType" value={eBaseName} />
          {eShareLevel == '2' ? <ETitle ecId={`${this && this.props && this.props.ecId || ''}_ETitle@7ujyq2`} ref="eTitle" eid={eid} value={eTitle} hasLink={false}  {...this.props}/> : ''}
          {Setting4User && Setting4User.props.params.isUser && Setting4User}
        </WeaSearchGroup>
        {eShareLevel == '2' && (
          <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@9f32w5`} title={getLabel(20885, '内容来源')} needTigger={true} showGroup={true}>
            {Setting4Admin}
          </WeaSearchGroup>
        )}
      </div>
    );
  }

    canSaveTemplate(){
      if (this.setting4user && this.setting4user.defaultProps && this.setting4user.defaultProps.canSaveTemplate) {
          const result = this.setting4user.defaultProps.canSaveTemplate
          return result
      }
      if (this.setting4admin && this.setting4admin.defaultProps && this.setting4admin.defaultProps.canSaveTemplate) {
          const result = this.setting4admin.defaultProps.canSaveTemplate
          return result
      }
      return false
  }

  async getSaveValues() {
    if (this.setting4user && this.setting4user.onSave && typeof this.setting4user.onSave == 'function') {
      const result = await this.setting4user.onSave();
      if (result == false) {
        return null;
      }
    }

    if (this.setting4admin && this.setting4admin.onSave && typeof this.setting4admin.onSave == 'function') {
      const result = await this.setting4admin.onSave();
      if (result == false) {
        return null;
      }
    }

    return {
      eContentTitle: this.refs['eTitle'] ? this.refs['eTitle'].getSaveValues() : this.props.eTitle
    };
  }
}
